//
//  FLTableViewCell.m
//  FeeLing
//
//  Created by 乔光玉 on 2019/1/2.
//  Copyright © 2019年 大仙儿飘. All rights reserved.
//

#import "FLTableViewCell.h"

@implementation FLTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    NSLog(@"%s", __func__);
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        
        WS(ws);
        self.bottomView = [[UIView alloc]init];
        self.bottomView.backgroundColor = [UIColor colorWithHexString:@"#2B2D5C"];
        
        [self.contentView addSubview:self.bottomView];
        
        [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(0);
            make.left.equalTo(ws.contentView.mas_left).with.offset(0);
            make.right.equalTo(ws.contentView.mas_right).with.offset(0);
            make.height.mas_equalTo(@66);
        }];
        
        self.line = [[UIView alloc]init];
        self.line.backgroundColor = [UIColor colorWithHexString:@"#353662"];
        [self.bottomView addSubview:self.line];
        [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(ws.bottomView.mas_bottom).with.offset(0);
            make.left.equalTo(ws.bottomView.mas_left).with.offset(0);
            make.right.equalTo(ws.bottomView.mas_right).with.offset(0);
            make.height.mas_equalTo(@1);
        }];
        
        self.mainView = [[UIView alloc] init];
        self.mainView.backgroundColor = [UIColor colorWithHexString:@"#24234F"];
        
        [self.contentView addSubview:self.mainView];
        
        
        
        
        
        
        
        
        
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.playButton setImage:[UIImage imageNamed:@"播放"] forState:UIControlStateNormal];
        [self.playButton setImage:[UIImage imageNamed:@"暂停"] forState:UIControlStateSelected];
        [self.playButton addTarget:self action:@selector(playButtonclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.playButton];
        [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(19);
            make.left.equalTo(ws.contentView.mas_left).with.offset(15);
            make.width.mas_equalTo(@32);
            make.height.mas_equalTo(@32);
        }];
        
        
        self.biaoti = [[UILabel alloc]init];
        self.biaoti.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        self.biaoti.textColor = [UIColor colorWithHexString:@"#FFFFFF"];
        self.biaoti.textAlignment = NSTextAlignmentLeft;
        self.biaoti.alpha = 1;
        self.biaoti.numberOfLines = 0;
        [self.contentView addSubview:self.biaoti];
        [self.biaoti mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(16);
            make.left.equalTo(ws.contentView.mas_left).with.offset(62);
            make.right.equalTo(ws.contentView.mas_right).with.offset(-87);
            make.height.mas_equalTo(@20);
        }];
        
        
        
        self.shijian = [[UILabel alloc]init];
        self.shijian.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        self.shijian.textColor = [UIColor colorWithHexString:@"#8184C1"];
        self.shijian.textAlignment = NSTextAlignmentLeft;
        self.shijian.alpha = 1;
        self.shijian.numberOfLines = 0;
        [self.contentView addSubview:self.shijian];
        [self.shijian mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(36);
            make.left.equalTo(ws.contentView.mas_left).with.offset(62);
            
            make.height.mas_equalTo(@20);
        }];
        //
        //
        self.listButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.listButton setImage:[UIImage imageNamed:@"更多-1"] forState:UIControlStateNormal];
        [self.listButton setImage:[UIImage imageNamed:@"更多-1"] forState:UIControlStateSelected];
        [self.listButton addTarget:self action:@selector(listButtonclick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:self.listButton];
        [self.listButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(10);
            make.right.equalTo(ws.contentView.mas_right).with.offset(-15);
            make.width.mas_equalTo(@30);
            make.height.mas_equalTo(@30);
        }];
        
        self.listLabel = [[UILabel alloc]init];
        self.listLabel.textAlignment = NSTextAlignmentCenter;
        self.listLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 10];
        self.listLabel.textColor = [UIColor colorWithRed:129/255.0 green:132/255.0 blue:193/255.0 alpha:1.0];
        
        self.listLabel.text = NSLocalizedString(@"moretitle", nil);
        [self.contentView addSubview:self.listLabel];
        [self.listLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.listButton.mas_bottom).with.offset(3);
            make.centerX.equalTo(ws.listButton.mas_centerX).with.offset(0);
            
        }];
        
       
        
        self.set =  [UIButton buttonWithType:UIButtonTypeCustom];
        [self.set setImage:[UIImage imageNamed:@"设置铃声"] forState:UIControlStateNormal];
        [self.set setImage:[UIImage imageNamed:@"设置铃声"] forState:UIControlStateSelected];
      
        [self.set addTarget:self action:@selector(setclick:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:self.set];
        
        [self.set mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.contentView.mas_top).with.offset(10);
            make.right.equalTo(ws.contentView.mas_right).with.offset(-56);
            make.width.mas_equalTo(@30);
            make.height.mas_equalTo(@30);
        }];
        
        self.setLabel = [[UILabel alloc]init];
        self.setLabel.textAlignment = NSTextAlignmentCenter;
        self.setLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 10];
        self.setLabel.textColor = [UIColor colorWithRed:129/255.0 green:132/255.0 blue:193/255.0 alpha:1.0];
        
        self.setLabel.text = NSLocalizedString(@"settitle", nil);
        [self.contentView addSubview:self.setLabel];
        [self.setLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(ws.set.mas_bottom).with.offset(3);
            make.centerX.equalTo(ws.set.mas_centerX).with.offset(0);
            
        }];
        
        
        
        
        self.share = [UIButton new];
        [self.share setImage:[UIImage imageNamed:@"分享未选中"] forState:UIControlStateNormal];
        [self.share setImage:[UIImage imageNamed:@"分享选中"] forState:UIControlStateSelected];
        [self.share setTitle:NSLocalizedString(@"sharetitle", nil) forState:UIControlStateNormal];
        [self.share setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        self.share.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 10];
        self.share.tag =1;
        self.share.hidden = YES;
        [self.share addTarget:self action:@selector(nameclick:) forControlEvents:UIControlEventTouchUpInside];
        self.share.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.share setTitleEdgeInsets:UIEdgeInsetsMake(self.share.imageView.frame.size.height + 30 ,-self.share.imageView.frame.size.width, 0.0,0.0)];
        CGSize shareSize = [NSLocalizedString(@"sharetitle", nil) boundingRectWithSize:CGSizeMake(SCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:10]} context:nil].size;
        //图片距离右边框距离减少图片的宽度，其它不边
        [self.share setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0, -self.share.titleLabel.bounds.size.width-shareSize.width)];
        
        [self.contentView addSubview:self.share];
        
        self.name = [UIButton new];
        [self.name setImage:[UIImage imageNamed:@"重命名未选中"] forState:UIControlStateNormal];
        [self.name setImage:[UIImage imageNamed:@"重命名选中"] forState:UIControlStateSelected];
        [self.name setTitle:NSLocalizedString(@"againametitle", nil) forState:UIControlStateNormal];
        [self.name setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        self.name.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 10];
        self.name.tag =2;
        self.name.hidden = YES;
        [self.name addTarget:self action:@selector(nameclick:) forControlEvents:UIControlEventTouchUpInside];
        self.name.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [self.name setTitleEdgeInsets:UIEdgeInsetsMake(self.name.imageView.frame.size.height + 30 ,-self.name.imageView.frame.size.width, 0.0,0.0)];
        CGSize nameSize = [NSLocalizedString(@"againametitle", nil) boundingRectWithSize:CGSizeMake(SCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:10]} context:nil].size;
        
        [self.name setImageEdgeInsets:UIEdgeInsetsMake(-10, 0.0,0, -self.name.titleLabel.bounds.size.width-nameSize.width)];
        
        [self.contentView addSubview:self.name];
        self.dele = [UIButton new];
        [self.dele setImage:[UIImage imageNamed:@"删除未选中"] forState:UIControlStateNormal];
        [self.dele setImage:[UIImage imageNamed:@"删除选中"] forState:UIControlStateSelected];
        [self.dele setTitle:NSLocalizedString(@"deletitle", nil) forState:UIControlStateNormal];
        [self.dele setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        self.dele.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 10];
        self.dele.tag =3;
        self.dele.hidden = YES;
        [self.dele addTarget:self action:@selector(nameclick:) forControlEvents:UIControlEventTouchUpInside];
        self.dele.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        CGSize deleSize = [NSLocalizedString(@"deletitle", nil) boundingRectWithSize:CGSizeMake(SCREENWIDTH, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:10]} context:nil].size;
        [self.dele setTitleEdgeInsets:UIEdgeInsetsMake(self.dele.imageView.frame.size.height+30 ,-self.dele.imageView.frame.size.width, 0.0,0.0)];
        
        //图片距离右边框距离减少图片的宽度，其它不边
        [self.dele setImageEdgeInsets:UIEdgeInsetsMake(-10,0,0,-self.dele.titleLabel.frame.size.width-deleSize.width)];
        [self.contentView addSubview:self.dele];
    }
    
    return self;
}
- (void)configHeight
{
    NSLog(@"%@----%s", self.isExpanded?@"yes":@"no", __func__);
    
    WS(ws);
    [self.bottomView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.contentView.mas_top).with.offset(0);
        make.left.equalTo(ws.contentView.mas_left).with.offset(0);
        make.right.equalTo(ws.contentView.mas_right).with.offset(0);
        if (self.isExpanded) {
            make.height.mas_equalTo(@132);
            
        }
        else {
            make.height.mas_equalTo(@66);
            
        }
        
        
    }];
    
    [self.mainView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(ws.contentView.mas_top).with.offset(66);
        make.left.equalTo(ws.contentView.mas_left).with.offset(0);
        make.right.equalTo(ws.contentView.mas_right).with.offset(0);
        
        if (self.isExpanded) {
            
            make.height.mas_equalTo(@66);
           
           
            self.share.hidden = NO;
            self.name.hidden = NO;
            self.dele.hidden = NO;
        }
        else {
            
            
            
            self.share.hidden = YES;
            self.name.hidden = YES;
            self.dele.hidden = YES;
        }
    }];
    
    
    
  
    
   
    [self.share mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.contentView.mas_left).with.offset(0);
        make.top.equalTo(ws.contentView.mas_top).with.offset(77);
        make.width.mas_equalTo(SCREENWIDTH/3);
        make.height.mas_equalTo(@44);
    }];
    
    [self.name mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.share.mas_right).with.offset(0);
        make.top.equalTo(ws.contentView.mas_top).with.offset(77);
        make.width.mas_equalTo(SCREENWIDTH/3);
        make.height.mas_equalTo(@44);
    }];
    
    [self.dele mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.name.mas_right).with.offset(0);
        make.top.equalTo(ws.contentView.mas_top).with.offset(77);
        make.width.mas_equalTo(SCREENWIDTH/3);
        make.height.mas_equalTo(@44);
    }];
    
}

- (void)refreshHeight
{
    self.isExpanded = !self.isExpanded;
    //    NSLog(@"%@", self.isExpanded?@"yes":@"no");
    
    [self configHeight];
    
    [self.contentView setNeedsUpdateConstraints];
    [self.contentView updateConstraintsIfNeeded];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.contentView layoutIfNeeded];
    }];
}

-(void)playButtonclick:(UIButton *)sender{
    
    self.playBlock(sender);
}

-(void)listButtonclick:(UIButton *)sender{
    
    self.listBlock(sender);
}
-(void)setclick:(UIButton *)sender{
    
    self.setBlock(sender);
}

-(void)nameclick:(UIButton *)sender{
    sender.selected = !sender.selected ;
    self.moreBlock(sender);
}
@end
